<?php

namespace App\Http;

use \App\Helpers\Utils;
use \App\Helpers\LoggerHelper;


/**
 * HTTP请求环境类
 * @package App\Http
 */
class Request extends \Phalcon\Http\Request {

    #region 变量

    /**
     * POST 请求参数
     * @var array
     */
    private $post = array();

    /**
     * GET 请求参数
     * @var array
     */
    private $queryString = array();

    /**
     * 请求参数
     * @var array
     */
    private $params;

    /**
     * 认证用户信息
     * @var array
     */
    private $user;

    #endregion

    #region 构造函数

    /**
     * 构造函数
     */
    function __construct() {

        // 初始化参数
        $this->_initParams();

    }

    #endregion

    #region 初始化

    /**
     * 初始化参数
     */
    private function _initParams() {

        /**
         * @var array|bool|\stdClass $json 被解析的Http json请求体
         */

        if ($this->isPost() || $this->isPut()) {
            // 获取被解析的Http
            $raw = $this->getRawBody();
            LoggerHelper::info("获取被解析的Http:" . $raw);
            if ($raw && !empty($raw)) {
                if (strpos($raw, "{") === 0 || strpos($raw, "[") === 0)
                    $this->post = json_decode($raw, true);
                elseif (strpos(strtolower($raw), "<xml>") === 0) {
                    $this->post = Utils::xmlToArray($raw);
                } else {
                    $this->post = $this->analysisQueryString($raw);
                }
            } elseif ($this->isPost()) {
                //获取Post数据
                $this->post = $this->getPost();
            }
        }

        // 获取查询参数
        $params = $this->getQuery();

        if (!empty($params)) {
            unset($params["_url"]);
            // 合并参数
            $this->queryString = !empty($this->params) ? array_merge($this->params, $params) : $params;
        }
    }

    #endregion

    #region 获取参数

    /**
     * 获取参数
     * @param $name 参数名称
     * @param boolean $filters 过滤器 包含的过滤器有："email","absint","int","int!","string","float","float!","alphanum","trim","striptags","lower","upper"
     * @param string $defVal 默认值
     * @param bool $noRecursive 是否递归
     * @return boolean | mixed
     */
    function getRequest($name = null, $filters = FALSE, $defVal = "", $noRecursive = false) {
        if ($name) {
            if (is_string($name)) {
                if (!empty($this->params) && isset($this->params[$name]))
                    if ($filters)
                        $param = $this->_filter->sanitize($this->params[$name], $filters, $noRecursive);
                    else
                        $param = $this->params[$name];
                return isset($param) && ($param || $param === "0") ? $param : $defVal;
            } elseif (is_array($name)) {
                $params = array();
                if (!empty($this->params)) {
                    foreach ($name as $key) {
                        $params[$key] = isset($this->params[$key]) ? $this->params[$key] : "";
                    }
                }
                return $params;
            }
        } else {
            return $this->params;
        }
    }

    /**
     * 获取认证用户信息
     * @param $name 参数名称
     * @return boolean | mixed
     */
    function getUser($name = null) {
        if ($name) {
            return isset($this->user[$name]) ? $this->user[$name] : ($name == "roles" ? array() : "");
        } else {
            return $this->user;
        }
    }

    /**
     * 解析查询参数
     * @param $queryString
     * @return array
     */
    function analysisQueryString($queryString) {
        $params = array();
        $data = explode("&", $queryString);
        if ($data) {
            foreach ($data as $item) {
                $arr = explode("=", $item);
                if (count($arr) == 2)
                    $params[$arr[0]] = urldecode($arr[1]);
            }
        }
        return $params;
    }

    #endregion

    #region 设置参数

    /**
     * 设置请求参数
     * @param $params
     */
    function set($params) {
        $this->params = $params;
    }

    /**
     * 追加参数数组
     * @param $params
     */
    function append($params) {
        if ($this->params)
            $this->params = array_merge($params, $this->params);
        else
            $this->params = $params;
    }

    /**
     * 获取POST参数
     * @param null $name
     * @param null $filters
     * @param null $defaultValue
     * @param bool $notAllowEmpty
     * @param bool $noRecursive
     * @return array|bool|mixed
     */
    function getPost($name = null, $filters = null, $defaultValue = null, $notAllowEmpty = false, $noRecursive = false) {
        if ($name) {
            return $this->getRequest($name, $filters, $defaultValue, $noRecursive);
        }
        return $this->post;
    }

    /**
     * 获取QueryString参数
     * @return array
     */
    function getQueryString() {
        return $this->queryString;
    }

    /**
     * 设置认证用户信息
     * @param $user 用户信息
     */
    function setUser($user) {
        $this->user = $user;
    }

    #endregion

}